Two types of data-manipulation:

  1. spatial data-manipulations: spatial cropping, union, aggregation, etc.
  2. ‘regular’ data-manipulations: combine categories, calculate percentage of population, etc.

… but same syntax and in one dataframe.

PS: always an option, do (2) in SAS, Stata, etc. before reading and joining data (previous step).

# library(rgdal)      # provides readOGR() to read in spatial data
# library(BelgiumMaps.StatBel)
# library(sf)
# library(tmap)       # plot thematic map with qtm()
# library(dplyr)      # general data-manipulation
# library(stringr)    # string-operations str_sub()
# library(readr)      # read CSV-file read_csv()
# load spatial data
raillines <- st_read(here('data/source/census_1851_raillines/1851EngWalesScotRail_Lines.shp')) %>%
  clean_names()
## Reading layer `1851EngWalesScotRail_Lines' from data source `/home/rstudio/projects/historical-maps-r/data/source/census_1851_raillines/1851EngWalesScotRail_Lines.shp' using driver `ESRI Shapefile'
## Simple feature collection with 1431 features and 2 fields
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: 155368 ymin: 36976.83 xmax: 655241.1 ymax: 805911.3
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs
# load data-of-interest

# join spatial data and data-of-interest

Data-manipulation

districts_pop <- districts_pop %>%
  mutate(pct_prof = tertiary_services_professions / total)
mapview(districts_pop, zcol = 'pct_prof')

Spatial manipulation

divisions <- districts_pop %>%
  group_by(r_div) %>%
  tally()
divisions
## Simple feature collection with 11 features and 2 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: 87019.07 ymin: 7067.26 xmax: 655747.5 ymax: 657473.5
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs
## # A tibble: 11 x 3
##    r_div           n                                               geometry
##  * <fct>       <int>                                         <GEOMETRY [m]>
##  1 EASTERN       153 MULTIPOLYGON (((576079.7 182724.1, 575881.1 182595.6,…
##  2 LONDON         50 MULTIPOLYGON (((545304.4 182108.8, 545328.8 182054.2,…
##  3 NORTH MIDL…    81 POLYGON ((538173.2 334349.2, 538357.7 334297.6, 53888…
##  4 NORTH WEST…    49 MULTIPOLYGON (((318709.4 387773.5, 318669.2 387723.4,…
##  5 NORTHERN       64 MULTIPOLYGON (((342348.8 478405.1, 342385.8 478359.1,…
##  6 SOUTH EAST…   196 MULTIPOLYGON (((448535.6 96647.07, 448662.4 96607.89,…
##  7 SOUTH MIDL…   111 MULTIPOLYGON (((516697 173206.1, 516698.8 173165.7, 5…
##  8 SOUTH WEST…   146 MULTIPOLYGON (((87825.76 8836.771, 87870.98 8816.206,…
##  9 WELSH          91 MULTIPOLYGON (((322121.3 165180.4, 322145.6 165022.8,…
## 10 WEST MIDLA…   158 MULTIPOLYGON (((359975 172330.1, 359944 172223.4, 359…
## 11 YORKSHIRE      99 MULTIPOLYGON (((446011.8 382146.7, 446017.4 382109, 4…
mapview(divisions)

Spatial / data-manipulation: select Manchester-region

nwestern <- districts_pop %>%
  filter(r_div == 'NORTH WESTERN')
mapview(nwestern, zcol = 'pct_secondary') + raillines
qtm(nwestern, fill = 'pct_secondary')
## Warning: The shape nwestern is invalid. See sf::st_is_valid

qtm(nwestern, fill = 'pct_secondary')
## Warning: The shape nwestern is invalid. See sf::st_is_valid